/*******************************************************************************
**
** Copyright (c) 2011 - The OWE Team
**
** This file is part of OWE.
**
** OWE is a free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; version
** version 2.1 of the License.
**
** OWE is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with OWE. If not, see <http://www.gnu.org/licenses/>.
**
*******************************************************************************/

#include <owe/authorelement.h>
#include <owe/configurationdocument.h>
#include <owe/descriptionelement.h>
#include <owe/nameelement.h>
#include <owe/widgetelement.h>
#include <owe/widgetinterface.h>

#include "widgetinterface_p.h"

OWE_USE_NAMESPACE

/*!
    \class WidgetInterface
    \brief W3C widget interface

    \ingroup Interfaces

    This class abstracts the widget as specified
    <a href="http://www.w3.org/TR/widgets-apis/">here</a>.
*/

WidgetInterfacePrivate::WidgetInterfacePrivate(const ConfigurationDocument &configuration, const char *locale)
    : valid(true)
    , configuration(&configuration)
    , locale(locale)
{
    if (!configuration.isValid()) {
        valid = false;
        return;
    }
}

WidgetInterfacePrivate::~WidgetInterfacePrivate()
{
}

UString WidgetInterfacePrivate::getElementText(const Element *element) const
{
    if (element)
        return element->text();

    return UString();
}

WidgetInterface::WidgetInterface(const ConfigurationDocument &configuration, const char *locale)
    : d_ptr(new WidgetInterfacePrivate(configuration, locale))
{
}

WidgetInterface::~WidgetInterface()
{
    delete d_ptr;
}

bool WidgetInterface::isValid() const
{
    return d_ptr->valid;
}

void WidgetInterface::setLocale(const char *locale)
{
    d_ptr->locale = locale;
}

UString WidgetInterface::author() const
{
    return d_ptr->getElementText(d_ptr->configuration->widgetElement()->author());
}

UString WidgetInterface::authorEmail() const
{
    return d_ptr->configuration->widgetElement()->author()->email();
}

UString WidgetInterface::authorHref() const
{
    return d_ptr->configuration->widgetElement()->author()->href();
}

UString WidgetInterface::description() const
{
    return d_ptr->getElementText(d_ptr->configuration->widgetElement()->description(d_ptr->locale));
}

UString WidgetInterface::id() const
{
    return d_ptr->configuration->widgetElement()->id();
}

UString WidgetInterface::name() const
{
    return d_ptr->getElementText(d_ptr->configuration->widgetElement()->name(d_ptr->locale));
}

UString WidgetInterface::shortName() const
{
    const NameElement *name = d_ptr->configuration->widgetElement()->name(d_ptr->locale);
    if (name)
        return name->short_();

    return UString();
}

PreferenceElementList WidgetInterface::preferences() const
{
    return d_ptr->configuration->widgetElement()->preference();
}

UString WidgetInterface::version() const
{
    return d_ptr->configuration->widgetElement()->version();
}

// FIXME: It should return as unsigned long
UString WidgetInterface::height() const
{
    return d_ptr->configuration->widgetElement()->height();
}

// FIXME: It should return as unsigned long
UString WidgetInterface::width() const
{
    return d_ptr->configuration->widgetElement()->width();
}
